package com.mayjean.nacos.operator;

import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import com.mayjean.nacos.data.GlobalDynamicData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author kyleju
 */
@Slf4j
@RequiredArgsConstructor
public class NacosOperator {
	/**
	 * global dynamic data
	 */
	private GlobalDynamicData globalDynamicData;
	/**
	 * nacos operator maintained
	 */
	private ConfigService configService;

	public NacosOperator(GlobalDynamicData globalRemoteData, ConfigService configService) {
		this.globalDynamicData = globalRemoteData;
		this.configService = configService;
	}

	/**
	 * get global dynamic data
	 *
	 * @return global dynamic data {@link GlobalDynamicData}
	 */
	public GlobalDynamicData getGlobalDynamicData() {
		return this.globalDynamicData;
	}

	public String getConfig(String dataId, String group, long timeoutMs) throws NacosException {
		return this.configService.getConfig(dataId, group, timeoutMs);
	}

	public String getConfigAndSignListener(String dataId, String group, long timeoutMs, Listener listener) throws NacosException {
		return this.configService.getConfigAndSignListener(dataId, group, timeoutMs, listener);
	}

	public void addListener(String dataId, String group, Listener listener) throws NacosException {
		this.configService.addListener(dataId, group, listener);
	}

	public boolean publishConfig(String dataId, String group, String content) throws NacosException {
		return this.configService.publishConfig(dataId, group, content);
	}

	public boolean removeConfig(String dataId, String group) throws NacosException {
		return this.configService.removeConfig(dataId, group);
	}

	public void removeListener(String dataId, String group, Listener listener) {
		this.configService.removeListener(dataId, group, listener);
	}

	public String getServerStatus() {
		return this.configService.getServerStatus();
	}
}
